package mainfuncs

import (
	"fmt"

	elastic "gopkg.in/olivere/elastic.v2"
)

var (
	EsClient *elastic.Client
)

type LogMessage struct {
	App     string   
	Topic   string
	Message string
}

//InitES 初始化ES
func InitES(addr string) (err error) {

	client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(addr))
	if err != nil {
		fmt.Println("connect es error", err)
		return
	}
	EsClient = client
	return
}

func sendToES(topic string, data []byte) (err error) {

	msg := &LogMessage{}
	msg.Topic = topic
	msg.Message = string(data)

	_, err = EsClient.Index().
		Index(topic).
		Type(topic).
		//Id(fmt.Sprintf("%d", i)).
		BodyJson(msg).
		Do()
	if err != nil {
		// Handle error
		panic(err)
		return
	}
	return
}
